from selenium import webdriver
import os, time, unittest
from HTMLTestRunner import HTMLTestRunner


class Baidu(unittest.TestCase):

    def setUp(self):
        chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"  # 浏览器驱动位置
        os.environ["webdriver.chrome.driver"] = chromedriver
        self.driver = webdriver.Chrome(chromedriver)
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        self.base_url = 'http://www.baidu.com'

    def test_baidu_search(self):
        '''百度搜索'''
        driver = self.driver
        driver.get(self.base_url + '/')
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("HTMLTestRunner")
        driver.find_element_by_id("su").click()
        time.sleep(2)
        title = driver.title
        self.assertEqual(title, 'unittest_百度搜索')

    def tearDown(self):
        self.driver.quit()


if __name__ == '__main__':
    testunit = unittest.TestSuite()
    testunit.addTest(Baidu("test_baidu_search"))
    now = time.strftime('%Y-%m-%d %H_%M_%S')
    fp = open('C:\\wksPy\\' + now + 'result.html', 'wb')
    runner = HTMLTestRunner(stream=fp, title='百度搜索', description='用例执行情况')
    runner.run(testunit)
    fp.close()
